package com.kkm.dao.Imp;

import com.kkm.dao.TypeDao;
import com.kkm.pojo.Type;
import com.kkm.util.JdbcUtils_Druid;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class TypeDaoImp implements TypeDao {
    private JdbcTemplate template=new JdbcTemplate(JdbcUtils_Druid.getDataSource());

    @Override
    public List<Type> showType(int index, int rows) {
        String sql="select * from type limit ?,?";
        return template.query(sql,new BeanPropertyRowMapper<Type>(Type.class),index,rows);
    }

    @Override
    public int checkCount() {
        String sql="select count(*) from type";
        return template.queryForObject(sql,int.class);
    }

    @Override
    public void addType(String tname) {
        String sql="insert into type values(null,?)";
        template.update(sql,tname);
    }

    @Override
    public List<Type> checkType(String checkName, int index, int rows) {
        String sql="select * from type where tname like ? limit ?,?";
        String like_name="%"+checkName+"%";
        return template.query(sql,new BeanPropertyRowMapper<Type>(Type.class),like_name,index,rows);
    }

    @Override
    public int checkCount(String checkName) {
        String sql="select count(*) from type where tname like ?";
        String like_name="%"+checkName+"%";
        int count=template.queryForObject(sql,int.class,like_name);
        return count;
    }

    @Override
    public void deleteType(int id) {
        String sql="delete from type where tid=?";
        template.update(sql,id);
    }

    @Override
    public void modifyType(String newName,int id) {
        String sql="update type set tname=? where tid=?";
        template.update(sql,newName,id);
    }
}
